﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Front_GetImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["UserID"] != null)
        {
            string strQuery = "Select * from User_Details where UserID=@UserID";
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyZuponsConnectionString2"].ConnectionString;
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@UserID", SqlDbType.VarChar).Value = Request.QueryString["UserID"];
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            DataTable dt = new DataTable();
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
            catch(Exception ex)
            {
                dt = null;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
            try
            {

                if (dt != null)
                {
                    if (dt.Rows[0]["UserImage"] != null)
                    {
                        Byte[] bytes = (Byte[])dt.Rows[0]["UserImage"];
                        Response.Buffer = true;
                        Response.Charset = "";
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.ContentType = "Image/JPG";
                        Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["FirstName"].ToString());
                        Response.BinaryWrite(bytes);
                        Response.Flush();
                        Response.End();
                        return;
                    }

                }
            }
            catch
            {
            }
           
                Response.WriteFile("Images/user_square.png");         
                Response.Flush();
                Response.End();

          
        }
    }
}
